if (typeof jQuery !== 'undefined') {
	(function($) {
		$('#spinner').ajaxStart(function() {
			$(this).fadeIn();
		}).ajaxStop(function() {
			$(this).fadeOut();
		});
	})(jQuery);
}


$(function(){
	var flag_expand=true;

	
//	<%-- Remove class task-full	--%>
	function removeTaskFull(){
		$('.task-group').children('.task').each(function(){																	
			$(this).removeClass('task-full');								
		})						
	}

//  <%-- Remove class task-full	--%>
	var stack_item_height = 30;
	var stack_small = 100;
	var stack_full  = 230;
	var basic_top = 30;	
	
	function arrange_issue(){						
		$(".task-group").each(function(){
			var col = $(this);
			//get column index
			var col_index = $(".task-group").index(this);
			var hightlight_index =999;
			var hightlight_column = 999;
			col.children(".task").each(function(){
			 	var ind = col.children(".task").index(this);
			 	
				if($(this).hasClass("task-full"))
				{
					hightlight_index = ind;
					hightlight_column = col_index;
				}
				if(hightlight_column == col_index && ind > hightlight_index)
					item_top = basic_top + stack_full - 50 + stack_item_height * ind+"px";
				else
					item_top = basic_top + stack_item_height * ind+"px";
				$(this).animate({top:item_top},100);
			});
		});
	}

	function getMaxHeightcolumn(){
		var maxHeight = 0;
		$('.column-kanban').each(function(){
			var colHeight = 0;
			$(this).children('.task-group').children('.task').each(function(){
				
				if($(this).index($(this)) != length - 1)
				{
					if($(this).hasClass("task-full"))
						colHeight += stack_full - 50;					
					else
						colHeight += stack_item_height;
				}
				else
				{
					if($(this).hasClass("task-full"))
						colHeight += stack_full ;					
					else
						colHeight += stack_small;
				};
			});				
			if(colHeight > maxHeight)
				maxHeight = colHeight;
		});
		return maxHeight;
	}

	function resizeHeightcolumn(){
		$('.column-kanban').css('height',getMaxHeightcolumn()+stack_full);
	}
	
// <%-- resize width column	--%>
	function resize(){
		var length = $(".status").length;
//		if(length<=6){
			$('.header-status').css({'width':100+'%'});
			$('.column').css({'width':100+'%'});						
			$('.status').each(function(){							
				$(this).css({
				    "width": 100/length+"%"
				});
			});
			$('.column-kanban').each(function(){
				$(this).css({
				    "width": 100/length+"%"
				});
			})
//		}						
	}
	
//	function resizeTask(droppableObj){
//		 var top_fix = 0;
//		 var length_child=droppableObj.children('span').length;
//		 if(length_child==1)
//			 top_fix=20;
//		 else
//			 top_fix=20*length_child;
//		 return top_fix;
//	}
	
//	<%--  FIRE EVENT	--%>
//	resizeHeightcolumn();
//	arrange_issue();
	resize();
	
	$('#setting-kanban input:checkbox').on('click',function(){
		var text = $(this).val();
		var id =  $(this).attr('id');						
		if($(this).is(':checked')){										
			$('#kanban').append('<div align="center" class="'+id+' column-kanban" style="width: 25%">'
								 +'<span><h3>'+text+'</h3></span></div>');								
			//resize();
		}
		else{
			$('.'+id+'').remove();
			//resize();							
		}						
	})
	
//	<%--	DRAP, DROP AND SORT	 --%>
	$(".task-group").sortable({
		connectWith: ".task-group",
		forcePlaceholderSize: true,
		tolerance: "pointer",
		zIndex: 9999,
		start: function(event, ui){
            $(ui.item).width($('.task-group .box').width());
        },
        stop: function(){
	        } 
		}).disableSelection();



//<%--		 SHOW FULL TASK 			--%>
	var flag_show = true;	
	function showFullTask(selector){
		removeTaskFull();
		if(flag_show){			
			selector.addClass('task-full');
			flag_show=false;
		}else{
			flag_show=true;
		}
		resizeHeightcolumn();
		arrange_issue();
		flag_expand=true;
	}
	
	$('.task').on('click',function(){
		showFullTask($(this));
	})

//<%--		 SHOW ICON ADDING 			--%>
	$('.column-kanban').mouseenter(function(){
		$(this).children('.add-task-icon').fadeIn();						
	})
	$('.column-kanban').mouseleave(function(){
		$(this).children('.add-task-icon').fadeOut();						
	})


//<%--		 CLICK ADD TASK 			--%>
		$('.add-task-icon').on('click',function(){		
			var task='<span id="task3" class="task" ><a href="#">789</a></span>';
			$(this).parent().children('.task-group').append(task);			
			arrange_issue();
			resizeHeightcolumn();			
		})
		
//		<%-- EXPAND	--%>  
	var i = 0;		
	var heightold = getMaxHeightcolumn();
	function expand(selector){			
		var heightnew = $('.column-kanban').height();
		var task_g = selector.parent();
		if(flag_expand){
			task_g.children('.task-group').children('.task').each(function(){							
				$(this).animate({'top':'+='+ i},200);
				i+=100;
			})
			flag_expand=false;
			heightnew += i;			
			i = 0;
		}
		else{																					
			task_g.children('.task-group').children('.task').each(function(){															
				$(this).animate({'top':'+='+ i},200);								
				i-=100;							
			})
			flag_expand=true;
			heightnew += i;
			i=0;
		}
		if(heightnew > heightold)
			$('.column-kanban').height(heightnew);
		else
			resizeHeightcolumn();
	}
	$('#expand').click(function(){
		expand($(this));
	});
})
